package leetcode.code1016;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public boolean queryString(String s, int n) {
		int len = s.length();
		if (n > len * len) {
			return false;
		}
		char[] cs = s.toCharArray();
		int[] nums = new int[len];
		for (int i = 0; i < len; i++) {
			nums[i] = cs[i] - '0';
		}
		boolean[] b = new boolean[n + 1];
		b[0] = true;
		int size = Integer.toBinaryString(n).length();
		int cnt = 0;
		for (int i = 1; i <= size; i++) {
			int window = (1 << i) - 1;
			int num = 0, p = 0;
			while (p < len) {
				num <<= 1;
				if (nums[p] == 1) {
					num |= 1;
				}
				num &= window;
				if (num <= n && !b[num]) {
					cnt++;
					b[num] = true;
				}
				p++;
			}
		}
		return cnt == n;
	}

	@Override
	public void debug4() {
		H.compare(true, this.queryString("0110", 3));
		H.compare(false, this.queryString("0110", 4));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
